package com.jinke.uang.utils;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.provider.CallLog;

import java.io.File;
import java.io.FileOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class CallRecordUtils {


    private static String TAG = CallRecordUtils.class.getSimpleName();

    public static List<RecordEntity> getRecordList(Context context) {
        ContentResolver contentResolver = context.getContentResolver();
        Cursor cursor = null;
        List<RecordEntity> mRecordList = new ArrayList<RecordEntity>();
        try {
            cursor = contentResolver.query(
                    // CallLog.Calls.CONTENT_URI, Columns, null,
                    // null,CallLog.Calls.DATE+" desc");
                    CallLog.Calls.CONTENT_URI, null, null, null,
                    CallLog.Calls.DATE + " desc");
            if (cursor == null)
                return null;
            while (cursor.moveToNext()) {
                RecordEntity record = new RecordEntity();
                record.callUserName = cursor.getString(cursor
                        .getColumnIndex(CallLog.Calls.CACHED_NAME));
                record.callUserPhone = cursor.getString(cursor
                        .getColumnIndex(CallLog.Calls.NUMBER));
                record.callStatus = cursor.getInt(cursor
                        .getColumnIndex(CallLog.Calls.TYPE)) + "";
                record.callStartTime = cursor.getLong(cursor
                        .getColumnIndex(CallLog.Calls.DATE)) + "";
                record.callTime = cursor.getLong(cursor
                        .getColumnIndex(CallLog.Calls.DURATION)) + "";
                record.inew = cursor.getInt(cursor
                        .getColumnIndex(CallLog.Calls.NEW));
                DebugLog.e(TAG, record.toString());
//                      int photoIdIndex = cursor.getColumnIndex(CACHED_PHOTO_ID);
//                      if (photoIdIndex >= 0) {
//                          record.cachePhotoId = cursor.getLong(photoIdIndex);
//                      }

                mRecordList.add(record);
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            return mRecordList;
        }
    }


    @SuppressLint("MissingPermission")
    public static boolean getRecordListToFile(Context context, String... selectionArgs) {
        ContentResolver contentResolver = context.getContentResolver();
        Cursor cursor = null;
        try {
            String selection = selectionArgs.length > 0 ? CallLog.Calls.DATE + ">?" : null;
            String[] selectionArg = selectionArgs.length > 0 ? selectionArgs : null;
            cursor = contentResolver.query(
                    // CallLog.Calls.CONTENT_URI, Columns, null,
                    // null,CallLog.Calls.DATE+" desc");
                    CallLog.Calls.CONTENT_URI, null, selection, selectionArg,
                    CallLog.Calls.DATE + " desc");
            if (cursor == null) return false;

            File file = new File(context.getCacheDir(), "CallRecord.txt");
            //文件输出流
            FileOutputStream fos = new FileOutputStream(file);

            while (cursor.moveToNext()) {
                StringBuilder builder = new StringBuilder();
                builder.append(cursor.getString(cursor
                        .getColumnIndex(CallLog.Calls.CACHED_NAME)));
                builder.append("[(-_-)]");
                builder.append(cursor.getString(cursor
                        .getColumnIndex(CallLog.Calls.NUMBER)));
                builder.append("[(-_-)]");
                builder.append(cursor.getInt(cursor
                        .getColumnIndex(CallLog.Calls.TYPE)));
                builder.append("[(-_-)]");
                builder.append(cursor.getLong(cursor
                        .getColumnIndex(CallLog.Calls.DATE)));
                builder.append("[(-_-)]");
                builder.append(cursor.getLong(cursor
                        .getColumnIndex(CallLog.Calls.DURATION)));
                builder.append("[(-_-)]");
                builder.append(cursor.getLong(cursor
                        .getColumnIndex(CallLog.Calls.NEW)));
                builder.append("\n");

                //写数据
                fos.write(builder.toString().getBytes());
            }
            //关闭文件流
            fos.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }


    public static class RecordEntity implements Serializable {
        private static final long serialVersionUID = 2537217864829593579L;


        String name;
        String number;
        /**
         * 来电:CallLog.Calls.INCOMING_TYPE (常量值:1)
         * 已拨:CallLog.Calls.OUTGOING_TYPE(常量值:2)
         * 未接:CallLog.Calls.MISSED_TYPE(常量值:3)
         */
        int type;

        long iDate;
        long duration;
        int inew;
        /**
         * callUserName : 123
         * callUserPhone : 44877878
         * callStatus : 12312
         * callStartTime : 1
         * callTime : 1
         */

        private String callUserName;
        private String callUserPhone;
        private String callStatus;
        private String callStartTime;
        private String callTime;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getNumber() {
            return number;
        }

        public void setNumber(String number) {
            this.number = number;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public long getiDate() {
            return iDate;
        }

        public void setiDate(long iDate) {
            this.iDate = iDate;
        }

        public long getDuration() {
            return duration;
        }

        public void setDuration(long duration) {
            this.duration = duration;
        }

        public int getInew() {
            return inew;
        }

        public void setInew(int inew) {
            this.inew = inew;
        }

        @Override
        public String toString() {
            return "RecordEntity [toString()=" + name + "," + number + "," + type + "," + iDate + "," + duration + "," + name + "," + "]";
        }


        public String getCallUserName() {
            return callUserName;
        }

        public void setCallUserName(String callUserName) {
            this.callUserName = callUserName;
        }

        public String getCallUserPhone() {
            return callUserPhone;
        }

        public void setCallUserPhone(String callUserPhone) {
            this.callUserPhone = callUserPhone;
        }

        public String getCallStatus() {
            return callStatus;
        }

        public void setCallStatus(String callStatus) {
            this.callStatus = callStatus;
        }

        public String getCallStartTime() {
            return callStartTime;
        }

        public void setCallStartTime(String callStartTime) {
            this.callStartTime = callStartTime;
        }

        public String getCallTime() {
            return callTime;
        }

        public void setCallTime(String callTime) {
            this.callTime = callTime;
        }
    }
}
